#coding:utf-8
from binary_tree import BinaryTree

class MyTree(BinaryTree):
    def __init__(self, item = None) -> None:
        super().__init__(item)

    def tree_append_leaf(self,leaf):
        """
        往boot节点下添加叶子
        返回挂载这个叶子的地址
        """
        if self.node is None:
            self.node = leaf
            return self
        item = MyTree(leaf)
        if self.lchild is not None:
            pnode = self.lchild
            while pnode.rchild is not None:
                pnode = pnode.rchild
            pnode.rchild = item
        else:
            self.lchild = item
        return item

    def tree_append_branch(self,branch):
        """
        往boot节点下添加树枝
        """        
        if self.lchild is not None:
            pnode = self.lchild
            while pnode.rchild is not None:
                pnode = pnode.rchild 
            pnode.rchild = branch        
            return   
        self.lchild = branch
 
    def tree_preorder(self,action):
        """先根遍历"""
        self.preorder(action)
        pass

    def tree_postorder(self,action):
        """后根遍历"""
        self.inorder(action)
        pass
    

if __name__ == "__main__":
    t = MyTree('a')
    # t = t0.tree_append_leaf('a')
    node_b = t.tree_append_leaf('b')
    node_c = t.tree_append_leaf('c')
    node_d = t.tree_append_leaf('d')

    node_b.tree_append_leaf('e')
    node_b.tree_append_leaf('f')
    node_c.tree_append_leaf('g')
    node_d.tree_append_leaf('h')
    node_d.tree_append_leaf('i')
    node_d.tree_append_leaf('j')

    t.tree_preorder(t.action)
    print("\r\n")
    t.tree_postorder(t.action)